package com.icez.calendar_view

import android.util.Log
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.abs

class Utils {
    companion object{
        /**
         * 获取指定日期的周几（0-周日 6-周六 5-周五 4-周四 3-周三 2-周二 1-周一）
         * @sample 
         * @author Icez
         */
        public fun getDayForWeek(year: Int,month: Int,day:Int): Int {
            val mCalendar = Calendar.getInstance()
            mCalendar[Calendar.YEAR] = year
            mCalendar[Calendar.MONTH] = month - 1
            mCalendar[Calendar.DATE] = day
            var weekDay = mCalendar.get(Calendar.DAY_OF_WEEK) - 1
            if(weekDay < 0){
                return 0
            }else{
                return weekDay
            }
        }

        /**
         *  获取指定月天数
         * @param year 年份
         * @param month 月份
         */
        public fun getDayForMonth(year: Int, month: Int): Int {
            val mCalendar = Calendar.getInstance()
            mCalendar[Calendar.YEAR] = year
            mCalendar[Calendar.MONTH] = month - 1
            mCalendar[Calendar.DATE] = 1 //把日期设置为当月第一天
            mCalendar.roll(Calendar.DATE, -1) //日期回滚一天，也就是最后一天
            return mCalendar[Calendar.DATE]
        }

        /**
         *  获取当前年份
         */
        public fun currentYear(): Int {
            val mCalendar: Calendar = Calendar.getInstance()
            return mCalendar.get(Calendar.YEAR)
        }

        /**
         *  获取当前月份
         */
        public fun currentMonth(): Int {
            val mCalendar: Calendar = Calendar.getInstance()
            return mCalendar.get(Calendar.MONTH) + 1
        }

        /**
         *  获取当前天
         */
        public fun currentDay(): Int {
            val mCalendar: Calendar = Calendar.getInstance()
            return mCalendar.get(Calendar.DATE)
        }


        /**
         *  事件字符串转时间戳
         * @param date 时间字符串
         * @param format 时间格式:（yyyy->年 MM->月 dd->日 hh->12小時 mm->分鐘 ss->秒 HH->24小時）
         */
        public fun dateStrToTimestamp(date: String, format: String): Long {
            try {
                var mSimpleDateFormat = SimpleDateFormat(format)
                return mSimpleDateFormat.parse(date).time
            } catch (ex: Exception) {
                return 0L
            }
        }

        /**
         * 计算时间相差的天数
         * @sample
         * @author Icez
         * @param timeMillis1 时间戳
         * @param timeMillis2 时间戳
         */
        public fun diffDayForTimestamp(timeMillis1: String?,
                                       timeMillis2: String?): Double {
            val diffTimestamp = abs((timeMillis1?.toLong()?:0) - (timeMillis2?.toLong()?:0)) /1000.0
            return diffTimestamp / (60 * 60 * 24)
        }



        /**
         *  是否在指定的时间内
         * @param timeMillis1 时间戳
         * @param timeMillis2 时间戳
         * @param targetTimeMillis 针对的时间戳
         */
        public fun isTimeInterval(
            timeMillis1: String?,
            timeMillis2: String?,
            targetTimeMillis: String? = ""
        ): Boolean {
            var mTargetTimeMillis = targetTimeMillis
            if (mTargetTimeMillis.isNullOrEmpty()) {
                mTargetTimeMillis = System.currentTimeMillis().toString()
            }
            if (timeMillis1.isNullOrEmpty() || timeMillis2.isNullOrEmpty()) {
                return false
            }
            if(timeMillis1.toLong()>timeMillis2.toLong()){
                if (getTargetDelCurrent(timeMillis1.toLong(), mTargetTimeMillis.toLong()) >= 0L &&
                    getTargetDelCurrent(mTargetTimeMillis.toLong(), timeMillis2.toLong()) >= 0L
                ) {
                    return true
                }
            }else{
                if (getTargetDelCurrent(mTargetTimeMillis.toLong(), timeMillis1.toLong()) >= 0L &&
                    getTargetDelCurrent(timeMillis2.toLong(), mTargetTimeMillis.toLong()) >= 0L
                ) {
                    return true
                }
            }

            return false
        }


        /**
         *  指定时间与当前时间的差
         * @param targetMillis 指定时间错 ，毫秒
         */
        public fun getTargetDelCurrent(targetMillis: Long, currentMillis: Long = 0): Long {
            var mCurrentMillis = currentMillis
            if (mCurrentMillis <= 0) {
                mCurrentMillis = System.currentTimeMillis()
            }
            return targetMillis - mCurrentMillis
        }
    }
}